import { defineStore } from 'pinia'
import $ajax from '@/api/to8030.js'
import router from '@/router/index.js'
import { showErrorMsg, showSuccessMsg } from '@/utils/alterMessage.js'
import { useLoginStore } from '@/stores/login/login.js'
import { useGridStore } from '@/stores/grid/grid.js'

export const useRegisterStore = defineStore('register', {
  state: () => ({
    /** @type{memId:number,memNumber:string,memName:string,memPwd:string,memSex:string,memBirthday:string,provinceId:number,cityId:number,address:string,roleId:number,memState:number,verifyCode:string,remarks:string} */
    member: {
      memId: -1,
      memNumber: '',
      memName: '',
      memPwd: '',
      memSex: '',
      memBirthday: '',
      province: '',
      address: '',
      remarks: '',
      detailedAddress: ''
    },
    onShow: true
  }),
  actions: {
    setMember(member) {
      this.member = member
    },
    clearMember() {
      this.member = {
        memId: -1,
        memNumber: '',
        memName: '',
        memPwd: '',
        memSex: '',
        memBirthday: '',
        province: '',
        address: '',
        remarks: '',
        detailedAddress: ''
      }
      const gridStore = useGridStore()
      gridStore.clearData()
    },
    addAddress() {
      this.member.address = this.member.province + '/' + this.member.detailedAddress
    },
    async register() {
      if (this.member.memId === -1) {
        const path = '/member/register'
        const res = await $ajax.post(path, this.member)
        if (res !== null && res.code != null && res.code === 200) {
          const loginStore = useLoginStore()
          loginStore.ruleForm.telephone = this.member.memNumber
          this.clearMember()
          showSuccessMsg('注册成功,请登录')
          await router.push('/login/TelAndPwdForm')
        } else {
          showErrorMsg('注册失败')
        }
      } else {
        const path = '/member/update'
        const res = await $ajax.put(path, this.member)
        if (res !== null && res.code != null && res.code === 200) {
          showSuccessMsg('更新成功')
        } else {
          showErrorMsg('更新信息失败')
          return false
        }
      }
    },
    async addMember() {
      this.addAddress()
      const path = '/member/register'
      const res = await $ajax.post(path, this.member)
      if (res !== null && res.code != null && res.code === 200) {
        return true
      } else {
        showErrorMsg('注册失败')
        return false
      }
    },
    async getMemberById(memId) {
      const path = '/member/select/id'
      const res = await $ajax.get(path, {
        params: {
          memId
        }
      })
      if (res !== null && res.code != null && res.code === 200) {
        this.member.memId = res.result.memId
        this.member.memNumber = res.result.memNumber
        this.member.memName = res.result.memName
        this.member.memSex = res.result.memSex
        this.member.memBirthday = res.result.memBirthday
        this.member.address = res.result.address
        this.member.remarks = res.result.remarks
        // 检查并处理address
        const addressParts = res.result.address.split('/')
        console.log(addressParts)
        if (addressParts.length > 0) {
          // 将最后一部分赋值给detailedAddress
          this.member.detailedAddress = addressParts.pop() // pop()移除并返回数组的最后一个元素
          // 剩余部分作为gridValue
          const gridStore = useGridStore()
          gridStore.gridValue = addressParts // 假设gridStore.gridValue期望接收一个数组
        }
      } else {
        showErrorMsg('获取成员信息失败')
      }
    },
    async loadMember() {
      let currentUser = sessionStorage.getItem('currentUser')
      if (currentUser === null) {
        router
          .push('/login')
          .then(() => {
            showErrorMsg('请先登录')
          })
          .catch((error) => {
            console.log(error)
          })
      }
      currentUser = JSON.parse(currentUser)
      this.getMemberById(currentUser.memId)
    }
  }
})
